
package com.sfdlJPush.controller;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.sfdlJPush.R;
import com.sfdlJPush.api.GetEnquiryListRequest;
import com.sfdlJPush.api.GetEnquiryListResponse;
import com.sfdlJPush.controller.adapter.MessageHistoryAdapter;
import com.sfdlJPush.controller.api.ApiListenrDefaultImpl;
import com.sfdlJPush.controller.api.B5MBaseResponse;
import com.sfdlJPush.model.Enquiry;
import com.sfdlJPush.utils.EventBus;
import com.sfdlJPush.utils.EventBus.SubscriberChangeListener;
import com.sfdlJPush.utils.EventNames;
import com.sfdlJPush.widget.pulltorefresh.PullToRefreshBase;
import com.sfdlJPush.widget.pulltorefresh.PullToRefreshBase.OnRefreshListener2;
import com.sfdlJPush.widget.pulltorefresh.PullToRefreshListView;

public class MessageHistoryFragment extends MainFragment implements SubscriberChangeListener
{
    private PullToRefreshListView mPullToRefreshListView;
    private MessageHistoryAdapter messageHistoryAdapter;
    private GetEnquiryListRequest mGetEnquiryListRequest;

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.message_history;
    }

    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        mPullToRefreshListView = (PullToRefreshListView) containerView.findViewById(R.id.pull_refresh_list);
        mPullToRefreshListView.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent , View view , int position , long id)
            {
                // TODO Auto-generated method stub
                Enquiry enquiry = messageHistoryAdapter.list.get((int) id);
                Bundle bundle = new Bundle();
                bundle.putSerializable("enquiry", enquiry);
                bundle.putInt("position", (int) id);
                replaceFragment(ViewEnquiryAndReplyFragment.class, bundle, true);
            }
        });
        mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>()
        {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView)
            {
                messageHistoryAdapter.list.clear();
                messageHistoryAdapter.notifyDataSetChanged();
                mGetEnquiryListRequest.firstPage();
                mGetEnquiryListRequest.start();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView)
            {
                mGetEnquiryListRequest.nextPage();
                mGetEnquiryListRequest.start();
            }
        });
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        messageHistoryAdapter = new MessageHistoryAdapter(mActivity, new ArrayList<Enquiry>());
        mPullToRefreshListView.setAdapter(messageHistoryAdapter);
        mGetEnquiryListRequest = new GetEnquiryListRequest(new ApiListenrDefaultImpl()
        {
            @Override
            public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                // TODO Auto-generated method stub
                super.onSuccess(b5mBaseResponse);
                GetEnquiryListResponse getEnquiryListResponse = (GetEnquiryListResponse) b5mBaseResponse;
                messageHistoryAdapter.list.addAll(getEnquiryListResponse.getCurrentList());
                messageHistoryAdapter.notifyDataSetChanged();
            }
        }, mProgressBar, mPullToRefreshListView);
    }

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
        mPullToRefreshListView.setRefreshing();
    }

    @Override
    public void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
        EventBus.getInstance().subscribe(EventNames.MESSAGE_NEW, this);
        EventBus.getInstance().subscribe(EventNames.NOTIFICATION_NEW, this);
        EventBus.getInstance().subscribe(EventNames.MESSAGE_HASREAD, this);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getInstance().unSubscribe(EventNames.MESSAGE_NEW, this);
        EventBus.getInstance().unSubscribe(EventNames.NOTIFICATION_NEW, this);
        EventBus.getInstance().unSubscribe(EventNames.MESSAGE_HASREAD, this);
    }

    @Override
    public void onSubscriberDataChanged(Object notificationName , Object notificateContent)
    {
        // TODO Auto-generated method stub
        if (EventNames.MESSAGE_NEW.equals(notificationName) || EventNames.NOTIFICATION_NEW.equals(notificationName))
        {
            mPullToRefreshListView.setRefreshing();
        }
        else if (EventNames.MESSAGE_HASREAD.equals(notificationName))
        {
            int position = (int) notificateContent;
            messageHistoryAdapter.getList().get(position).readFlag = "1";
            messageHistoryAdapter.notifyDataSetChanged();
        }
    }
}
